<template>
  <div>
    <el-dialog v-if="enable" :visible.sync="enable" title="文件下载" width="30%">
      <div style="margin-bottom:1em;">
        <div>本地文件：{{localFilePath}}</div>
        <div>远程路径：{{remoteFilePath}}</div>
      </div>
      <el-progress :text-inside="true" :stroke-width="26" :percentage="progress" :status="status"></el-progress>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="'success' != status" @click="actionCancel">取 消</el-button>
        <el-button type="primary" v-if="'success' == status" @click="actionClose">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
const fs = require("fs");
const {dialog} = require('@electron/remote')
export default {
    name : 'DialogFileUpload',
    data() {
        return {
            enable : false,
            localFilePath : null,
            remoteFilePath : null,
            fileSize : 0,
            uploadedSize : 0,
            status : 'success',
            readStream : null,
            writeStream : null,
        };
    },
    computed : {
        // 进度百分比
        progress() {
            if ( 0 == this.fileSize ) {
                return 0;
            }
            let val = (this.uploadedSize / this.fileSize * 100).toFixed(2);
            return parseFloat(val);
        }
    },
    methods : {
        /**
         * 上传文件
         * @param {Object} file
         */
        start( path ) {
            this.localFilePath = dialog.showOpenDialogSync({ properties: ['openFile'] });
            if ( undefined == this.localFilePath ) {
                return;
            }
            this.localFilePath = this.localFilePath[0];

            let $this = this;
            fs.stat(this.localFilePath,function(err,stats){
                $this.fileSize = stats.size;
            });
            
            let filename = this.localFilePath.split('\\');
            filename = filename[filename.length - 1];
            
            this.remoteFilePath = path.slice();
            this.remoteFilePath.push(filename);
            this.remoteFilePath = '/' + this.remoteFilePath.join('/');

            this.upload();
            this.status = null;
            this.uploadedSize = 0;
            this.enable = true;
        },

        // 上传文件
        async upload() {
            let sftp = await this.$store.getters.serverCurrent.getSftp();
            this.writeStream = sftp.createWriteStream(this.remoteFilePath);

            let $this = this;
            this.readStream = fs.createReadStream(this.localFilePath);
            this.readStream.on('data',function (chunk) {
                console.log('data');
                $this.uploadedSize += chunk.length;
                $this.writeStream.write(chunk);
            });
            this.readStream.on('end',function (chunk) {
                console.log('end');
                $this.status = 'success';
                $this.writeStream.end();
                $this.$emit('uploaded');
            });
            this.readStream.on('error',function (err) {
               console.log("error");
            });
        },

        // 关闭弹框
        actionClose() {
            this.enable = false;
        },

        // 取消下载
        actionCancel() {
            this.writeStream.end();
            this.readStream.close();
            this.enable = false;
        },
    }
}
</script>